package net.a6polo.a6poloweibo;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class DetailTabActivity extends TabActivity {

	private static final String CONTENT_TAB = "content";
	private static final String COMMENT_TAB = "comment";
	private static final String REPOST_TAB = "repost";

	private TabHost tabHost;
	private String weiboId = "";

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_tab);
		
		ActionBar actionBar = this.getActionBar();
		actionBar.setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP, ActionBar.DISPLAY_HOME_AS_UP);

		tabHost = this.getTabHost();

		Intent intent = getIntent();
		weiboId = intent.getStringExtra("weiboId");
		Intent contentIntent = new Intent();
		contentIntent.putExtra("weiboId", weiboId);
		contentIntent.setClass(DetailTabActivity.this, WeiboDetailsActivity.class);

		Intent commentIntent = new Intent();
		commentIntent.putExtra("weiboId", weiboId);
		commentIntent.setClass(DetailTabActivity.this, CommentActivity.class);

		Intent repostIntent = new Intent();
		repostIntent.putExtra("weiboId", weiboId);
		repostIntent.setClass(DetailTabActivity.this, RepostActivity.class);

		TabSpec contentSpec = tabHost.newTabSpec(CONTENT_TAB).setIndicator(CONTENT_TAB).setContent(contentIntent);
		TabSpec commentSpec = tabHost.newTabSpec(COMMENT_TAB).setIndicator(COMMENT_TAB).setContent(commentIntent);
		TabSpec repostSpec = tabHost.newTabSpec(REPOST_TAB).setIndicator(REPOST_TAB).setContent(repostIntent);

		tabHost.addTab(contentSpec);
		tabHost.addTab(commentSpec);
		tabHost.addTab(repostSpec);

		RadioGroup radioGroup = (RadioGroup) this.findViewById(R.id.rg_detail_btns);

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {

				switch (checkedId) {
				case R.id.rd_content:
					tabHost.setCurrentTabByTag(CONTENT_TAB);
					break;

				case R.id.rd_comment:
					tabHost.setCurrentTabByTag(COMMENT_TAB);
					break;

				case R.id.rd_repost:
					tabHost.setCurrentTabByTag(REPOST_TAB);
					break;

				default:
					break;
				}

			}
		});
	}

	@Override  
	public boolean onCreateOptionsMenu(Menu menu) {  
	    MenuInflater inflater = getMenuInflater();  
	    inflater.inflate(R.menu.actionbar_weibo_detail, menu);  
	    return true;  
	} 
	
	@Override

	public boolean onOptionsItemSelected(MenuItem item) {

	    switch (item.getItemId()) {

	        case android.R.id.home:
	            Intent intent = new Intent(this, MainActivity.class);
	            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	            startActivity(intent);
	            return true;
	        case R.id.menu_repost:
	            Intent repostIntent = new Intent(this, CommentWeiboactivity.class);
	            repostIntent.putExtra("sourceId", weiboId);
	            repostIntent.putExtra("type", "repost");
	            startActivity(repostIntent);
	            return true;
	        case R.id.menu_comment:
	        	 Intent commentIntent = new Intent(this, CommentWeiboactivity.class);
	        	 commentIntent.putExtra("sourceId", weiboId);
	        	 commentIntent.putExtra("type", "comment");
		         startActivity(commentIntent);
	            return true;

	        default:
	            return super.onOptionsItemSelected(item);

	    }

	}
}
